/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.goods.appweb;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.responseReturn.entity.ResultObject;
import com.jeesite.common.responseReturn.utils.CommonUtils;
import com.jeesite.common.responseReturn.web.Render;
import com.jeesite.common.utils.CommonPath;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.config.Contacts;
import com.jeesite.modules.config.MyProps;
import com.jeesite.modules.goods.dao.ZtSpecificationsDao;
import com.jeesite.modules.goods.entity.Brand;
import com.jeesite.modules.goods.entity.GoodsType;
import com.jeesite.modules.goods.entity.ZtBackImg;
import com.jeesite.modules.goods.entity.ZtBanner;
import com.jeesite.modules.goods.entity.ZtCard;
import com.jeesite.modules.goods.entity.ZtColor;
import com.jeesite.modules.goods.entity.ZtGoods;
import com.jeesite.modules.goods.entity.ZtGoodsquan;
import com.jeesite.modules.goods.entity.ZtOrder;
import com.jeesite.modules.goods.entity.ZtQuan;
import com.jeesite.modules.goods.entity.ZtQuanself;
import com.jeesite.modules.goods.entity.ZtSpecifications;
import com.jeesite.modules.goods.entity.ZtTrolley;
import com.jeesite.modules.goods.service.BrandService;
import com.jeesite.modules.goods.service.GoodsTypeService;
import com.jeesite.modules.goods.service.ZtBackImgService;
import com.jeesite.modules.goods.service.ZtBannerService;
import com.jeesite.modules.goods.service.ZtCardService;
import com.jeesite.modules.goods.service.ZtColorService;
import com.jeesite.modules.goods.service.ZtCommentService;
import com.jeesite.modules.goods.service.ZtCompanyService;
import com.jeesite.modules.goods.service.ZtGoodsService;
import com.jeesite.modules.goods.service.ZtGoodscollService;
import com.jeesite.modules.goods.service.ZtGoodsquanService;
import com.jeesite.modules.goods.service.ZtOrderService;
import com.jeesite.modules.goods.service.ZtQuanselfService;
import com.jeesite.modules.goods.service.ZtSpecificationsService;
import com.jeesite.modules.goods.service.ZtTipService;
import com.jeesite.modules.goods.service.ZtTrolleyService;
import com.jeesite.modules.jdProduct.utils.ProductPoolUtils;
import com.jeesite.modules.pay.wxpay.api.CommonUtil;
import com.jeesite.modules.sys.service.UserService;
import com.jeesite.modules.util.AliRequestUtils;
import com.jeesite.modules.util.Picture1;
import com.jeesite.modules.util.Picture2;
import com.jeesite.modules.util.TTest;
import com.jeesite.modules.xcxAdmin.entity.ZtAdmin;
import com.jeesite.modules.xcxAdmin.entity.ZtGoodsqcode;
import com.jeesite.modules.xcxAdmin.service.ZtAdminService;
import com.jeesite.modules.xcxAdmin.service.ZtGoodsqcodeService;


/**
 * 测试数据Controller
 * 
 * @author ThinkGem
 * @version 2018-04-22
 */
@Controller
@RequestMapping(value = "${frontPath}/ztGoods")
public class AppZtGoodsController extends BaseController {

	@Autowired
	private ZtGoodsService ztGoodsService;
	@Autowired
	private ZtGoodscollService ztGoodscollService;
	@Autowired
	ZtTrolleyService ztTrolleyService;
	@Autowired
	private ZtBannerService ztBannerService;
	
	@Autowired
	private ZtCompanyService ztCompanyService;

	@Autowired
	private GoodsTypeService goodsTypeService;
	@Autowired
	private ZtSpecificationsService ztSpecificationsService;

	@Autowired
	private ZtBackImgService ztBackImgService;
	@Autowired
	private UserService userService;
	@Autowired
	private BrandService brandService;
	 
	@Autowired
	ZtQuanselfService ztQuanselfService;
	
	@Autowired
	ZtCommentService ztCommentService;
	
	@Autowired
	ZtAdminService ztAdminService;
	@Autowired
	ZtGoodsquanService ztGoodsquanService;

	@Autowired
	private ZtOrderService ztOrderService;
	
	@Autowired
	private ZtColorService ztColorService;
	
	@Autowired
	private ZtTipService ztTipService;

	@Autowired
	private MyProps myProps;
	@Autowired
	ZtGoodsqcodeService ztGoodsqcodeService;
	@Autowired
	ZtCardService ztCardService;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public ZtGoods get(String id, boolean isNewRecord) {
		return ztGoodsService.get(id, isNewRecord);
	}
	
	
	/**
	 * 查询列表数据
	 */
	@RequestMapping(value = "ztCard")
	@ResponseBody
	public void ztCard(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		ZtCard ztCard=ztCardService.getOneCard();
		if(StringUtils.isNotBlank(ztCard.getImg())&&!ztCard.getImg().startsWith("http")){
			ztCard.setImg(myProps.getServerPath()+ztCard.getImg());
		}
		ResultObject res = new ResultObject();
		res.setResult(200L);
		res.setData(ztCard);
		Render.renderString(response, res);
	}
	
	/**
	 * 查询列表数据
	 */
	@RequestMapping(value = "search")
	@ResponseBody
	public void search(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		String paramStr = CommonUtils.getParams(request);

		JSONObject jo = JSONObject.parseObject(paramStr);
		String title = jo.getString("title");
		
		String pageNum = "1";
		
		ZtAdmin ztAdmin=new ZtAdmin();
		if(jo.get("adminid")!=null){
			String adminid=jo.getString("adminid");
			ztAdmin.setId(adminid);
			ztAdmin=ztAdminService.get(ztAdmin);
		}
		
		if(jo.get("pageNum")!=null){
			pageNum=jo.getString("pageNum");
		}

		ResultObject res = new ResultObject();
		ZtGoods ztGoods=new ZtGoods();
		
		if(jo.get("brandid")!=null){
			String brandid=jo.getString("brandid");
			ztGoods.setGoodstemp("3");// 
			ztGoods.setBrandid(brandid);
		}
		
		ztGoods.setAdminLevel("0");
		ztGoods.setTitle(title);
		ztGoods.setPageNum((Integer.valueOf(pageNum)-1)*15);
		List<Map> ztGoodsList = ztGoodsService.findMh(ztGoods);
		ztGoodsList=jdCheck(ztGoodsList);
		res.setResult(200L);
		res.setData(ztGoodsList);
		Render.renderString(response, res);
	}
	
	/**
	 * 查询列表数据
	 */
	@RequestMapping(value = "typeSearch")
	@ResponseBody
	public void typeSearch(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		String paramStr = CommonUtils.getParams(request);

		JSONObject jo = JSONObject.parseObject(paramStr);
		String title = jo.getString("title");
		
		String pageNum = "1";
		
		ZtAdmin ztAdmin=new ZtAdmin();
		if(jo.get("adminid")!=null){
			String adminid=jo.getString("adminid");
			ztAdmin.setId(adminid);
			ztAdmin=ztAdminService.get(ztAdmin);
		}
		
		if(jo.get("pageNum")!=null){
			pageNum=jo.getString("pageNum");
		}

		ResultObject res = new ResultObject();
		ZtGoods ztGoods=new ZtGoods();
		
		if(jo.get("typeid")!=null){
			String typeid=jo.getString("typeid");
			ztGoods.setType(","+typeid+",");
		}
		ztGoods.setGoodstemp("0");// 
		ztGoods.setAdminLevel("0");
		ztGoods.setTitle(title);
		ztGoods.setPageNum((Integer.valueOf(pageNum)-1)*15);
		List<Map> ztGoodsList = ztGoodsService.findMhByType(ztGoods);
		ztGoodsList = jdCheck(ztGoodsList);
		res.setResult(200L);
		res.setData(ztGoodsList);
		Render.renderString(response, res);
		
	}
	
	
	/**
	 * 活动类型
	 */
	@RequestMapping(value = "list")
	@ResponseBody
	public void listData(ZtGoods ztGoods, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		
		ResultObject res = new ResultObject();
		Map map = new HashMap();

		ZtBanner ztBanner = new ZtBanner();
		ztBanner.setType("0");
		
		String paramStr = CommonUtils.getParams(request);
		ZtAdmin ztAdmin=new ZtAdmin();
		
		JSONObject jo = JSONObject.parseObject(paramStr);
			
		if(jo!=null && jo.get("adminid")!=null){
			String adminid=jo.getString("adminid");
			ztAdmin.setId(adminid);
			ztAdmin=ztAdminService.get(ztAdmin);
		}
		
		List<ZtBanner> bannerList = ztBannerService.findMh(ztBanner);
		for (ZtBanner zd : bannerList) {
			if(StringUtils.isNotBlank(zd.getImg())&&!zd.getImg().startsWith("http")){
			//zd.setImg(myProps.getServerPath()+zd.getImg());
			zd.setImg("http://system365.oss-cn-shenzhen.aliyuncs.com/"+zd.getImgname());
			// http://system365.oss-cn-shenzhen.aliyuncs.com/kk.png
			
			}
		}
		List<Map> typeList=new ArrayList<Map>();
		List<Map> typeMainList=new ArrayList<Map>();
		if(StringUtils.isNotBlank(ztAdmin.getCompany())&&!"0".equals(ztAdmin.getCompany())){
			String goodscode=ztCompanyService.getByCompanyStr(ztAdmin.getCompany());
			if(StringUtils.isNotBlank(goodscode)){
				String[] typeArr=goodscode.split(",");
				for(String str:typeArr){
					Map listMap=goodsTypeService.getListByCode(str);
					if(listMap!=null && listMap.size()>0){
						typeList.add(listMap);
					}
					Map mainListMap=goodsTypeService.getMainListByCode(str);
					if(mainListMap!=null && mainListMap.size()>0){
						typeMainList.add(mainListMap);
					}
				}
			}
		}else{
			typeList = goodsTypeService.getList();
			typeMainList = goodsTypeService.getMainList();
		}
		//商品分类
		
		
		for (Map m : typeList) {
			if(m.get("img")!=null && StringUtils.isNotBlank(m.get("img").toString())&&!m.get("img").toString().startsWith("http")){
			//m.put("img",myProps.getServerPath()+m.get("img").toString());
				m.put("img","http://system365.oss-cn-shenzhen.aliyuncs.com/"+m.get("imgname").toString());
			}
		}
		//主页分类
	
		
		//今日好赚
		ztGoods.setGoodstemp("1");// 
		ztGoods.setPageNum(0);
		ztGoods.setAdminLevel("0");
		
		List<Map> haozhuan = ztGoodsService.findMainMh(ztGoods);
		haozhuan = jdCheck(haozhuan);

		
		//精选好物
		ztGoods.setGoodstemp("2");// 
		ztGoods.setPageNum(0);
		ztGoods.setAdminLevel("0");
		List<Map> jinxuan = ztGoodsService.findMainMh(ztGoods);
		jinxuan = jdCheck(jinxuan);
		
		ztGoods.setGoodstemp("0");// 
		ztGoods.setPageNum(0);
		List<Map> fenlei =new ArrayList<Map>();
		if(typeMainList!=null && typeMainList.size()>0){
		ztGoods.setType(typeMainList.get(0).get("id").toString()+",");

		if(StringUtils.isNotBlank(ztAdmin.getLevel())){
			if(ztAdmin.getLevel().equals("0")){
				ztGoods.setAdminLevel("0");
			}else{
				ztGoods.setAdminLevel("1");
			}
		}else {
			ztGoods.setAdminLevel("0");
		}
		 fenlei = ztGoodsService.findMhByTypeByFirst(ztGoods);
		 fenlei=jdCheck(fenlei);

	}
		
		
		if("0".equals(ztAdmin.getLevel())){//非店主
			
		ZtCard ztCard=ztCardService.getOneCard();
		if(StringUtils.isNotBlank(ztCard.getImg())&&!ztCard.getImg().startsWith("http")){
		
			ztCard.setImg("http://system365.oss-cn-shenzhen.aliyuncs.com/"+ztCard.getImgname());
		}
		map.put("ztCard", ztCard);
		}
		
		ZtBackImg ztBackImg=new ZtBackImg();
		ztBackImg.setType("1");
		ztBackImg=ztBackImgService.getMh(ztBackImg);
		if(StringUtils.isNotBlank(ztBackImg.getImg())&&!ztBackImg.getImg().startsWith("http")){
			
			ztBackImg.setImg("http://system365.oss-cn-shenzhen.aliyuncs.com/"+ztBackImg.getImgname());

		}
		map.put("haozhuanBack", ztBackImg);
		map.put("haozhuan", haozhuan);
		map.put("jinxuan", jinxuan);
		map.put("typeList", typeList);
		
		map.put("bannerList", bannerList);
		map.put("typeMainList", typeMainList);
		
		map.put("fenlei", fenlei);

		res.setResult(200L);
		res.setData(map);
		Render.renderString(response, res);
	}

	/**
	 * 获取订单的优惠 
	 */
	@ResponseBody
	@RequestMapping(value = "getGoodsOrg")
	public void getGoodsOrg(Model model, HttpServletRequest request,
			HttpServletResponse response) {
		
		JSONArray ja=new JSONArray();
		Double priceOrg=0D;//优惠的价格
		
		
		String paramStr = CommonUtils.getParams(request);

		JSONObject jo = JSONObject.parseObject(paramStr);
		String goodsid_str = jo.getString("goodsids");//多商品    |   分隔开来
		String adminid=jo.getString("adminid");	
		
		ZtQuanself ztQuanself = new ZtQuanself();
	
		ZtAdmin ztAdmin = new ZtAdmin();
		ztAdmin.setId(adminid);
		ztQuanself.setZtAdmin(ztAdmin);
	
		ztQuanself.setUsestatus("0");//未使用    1已使用
			
		List<ZtQuanself> page = ztQuanselfService.findList(ztQuanself);
			
		List<ZtQuanself> zqsList=new ArrayList<ZtQuanself>();//我的有效的券
		
		if (page != null && page.size() > 0) {
			for (ZtQuanself zd : page) {
				
				ZtQuan ztQ = zd.getZtQuan();
				
				int comptime=com.jeesite.modules.pay.totalPay.util.DateUtils.compare_date(new Date(), ztQ.getToTime());
				if(comptime==-1&&("0".equals(ztQ.getUsestatus()))&&ztQ.getOverCount()>0){//券有效
					
					zqsList.add(zd);
				}

			}
		}
		
		ResultObject res = new ResultObject();
		String[] goodsArr=goodsid_str.split("\\|");
		for(String goodsid:goodsArr){
			ZtGoods ztGoods = new ZtGoods();
			if(goodsid.length()<12){
				ZtGoodsqcode ztGoodsqcode=new ZtGoodsqcode();
				ztGoodsqcode.setId(goodsid);
				ztGoodsqcode=ztGoodsqcodeService.getByBid(ztGoodsqcode);
				if(ztGoodsqcode!=null){
					
					goodsid=ztGoodsqcode.getGoodsid();
					
				}
			}
			ztGoods.setId(goodsid);
			ztGoods = ztGoodsService.get(ztGoods);
			
		
			ZtGoodsquan ztGoodsquan=new ZtGoodsquan();
			ztGoodsquan.setZtGoodsid(ztGoods.getId());
			List<ZtGoodsquan> ztQuanList = ztGoodsquanService.findList(ztGoodsquan);
		
			
				if(ztQuanList!=null && ztQuanList.size()>0){
					if(zqsList!=null && zqsList.size()>0){
						//String quanselfid="";
						ZtQuanself zsf=null;
						for(ZtGoodsquan zgq:ztQuanList){
					
					
							 zsf= getQuanAndPrice(zqsList,zgq.getZtQuan().getId());
							 
					if(zsf!=null){
						priceOrg+=zgq.getZtQuan().getPrice();
						break;
					}
					
				}
				JSONObject job=new JSONObject();
				job.put("goodsid",goodsid);
				/*Map m=new HashMap();
				
				if(zsf!=null){
					m.put("quanselfid", zsf.getId());
					m.put("toTime", DateUtils.formatDate(zsf.getZtQuan().getToTime(), "MM月dd日 HH:mm"));
					m.put("price", zsf.getZtQuan().getPrice());
					m.put("name", zsf.getZtQuan().getName());
				}*/
				if(zsf!=null && StringUtils.isNotBlank(zsf.getId())){
					job.put("quanselfid",zsf.getId());
				}else{
					job.put("quanselfid","");
				}
				
				ja.add(job);
				}else{
					JSONObject job=new JSONObject();
					job.put("goodsid",goodsid);
					job.put("quanselfid","");
					ja.add(job);
				}
				}else{
					JSONObject job=new JSONObject();
					job.put("goodsid",goodsid);
					job.put("quanselfid","" );
					ja.add(job);
					
				}
			
		}
		JSONObject jsonObj=new JSONObject();
		jsonObj.put("result", 200L);
		jsonObj.put("data", ja);
		jsonObj.put("priceOrg", priceOrg);
		
		try {
			response.reset();
	        response.setContentType("application/json");
	        response.setCharacterEncoding("utf-8");
			response.getWriter().print(jsonObj.toJSONString());
			
		} catch (IOException e) {
			
		}
		
	
	}
	
	
	public ZtQuanself getQuanAndPrice(List<ZtQuanself> zqsList,String quanid){
		//zgq.getZtQuan().getId()
		ZtQuanself zsf=null;
		for(ZtQuanself zf:zqsList){
			if(quanid.equals(zf.getZtQuan().getId())){
				//quanselfid=zf.getId(); 
				zsf=zf;
				
				break;
			}
		}
		return zsf;
		
	}
	/**
	 * 查看编辑表单
	 */
	@ResponseBody
	@RequestMapping(value = "detail")
	public void form(Model model, HttpServletRequest request,
			HttpServletResponse response) {
		String paramStr = CommonUtils.getParams(request);

		JSONObject jo = JSONObject.parseObject(paramStr);
		String goodsid = jo.getString("goodsid").toString();
		
		String lastadmin=""; 
		if(jo.get("lastadmin")!=null){
			lastadmin=jo.get("lastadmin").toString();
		}
		
		if(StringUtils.isNotBlank(goodsid)&&"rz".equals(goodsid)){
			ZtGoodsqcode ztGoodsqcode=new ZtGoodsqcode();
			if(StringUtils.isNotBlank(lastadmin)){
			ztGoodsqcode.setBid(Integer.valueOf(lastadmin));
			ztGoodsqcode=ztGoodsqcodeService.getByBid(ztGoodsqcode);
			if(ztGoodsqcode!=null){
				goodsid=ztGoodsqcode.getGoodsid();
				lastadmin=ztGoodsqcode.getAdminid();
				
			}
			}
		}
		
		ResultObject res = new ResultObject();

		ZtGoods ztGoods = new ZtGoods();
		ZtAdmin ztAdmin=new ZtAdmin();
		if(jo.get("adminid")!=null){
			String adminid=jo.getString("adminid");
			ztAdmin.setId(adminid);
			ztAdmin=ztAdminService.get(ztAdmin);
			
			if(lastadmin!=null && StringUtils.isNotBlank(lastadmin)&& !ztAdmin.getId().equals(lastadmin)){
				if("0".equals(ztAdmin.getLevel())){
					if(StringUtils.isNoneBlank(ztAdmin.getLastadmin())){//有上级的时候
						
						ZtAdmin la=new ZtAdmin();
						la.setId(ztAdmin.getLastadmin());
						la=ztAdminService.get(la);
						if("0".equals(la.getLevel())){
							ZtAdmin las=new ZtAdmin();
							las.setId(lastadmin);
							las=ztAdminService.get(las);
							if(!"0".equals(las.getLevel())){
								ztAdmin.setLastadmin(lastadmin);
								ztAdmin.setYaoqingDate(new Date());
								
								if("1".equals(ztAdmin.getMemtype())){
									if("0".equals(ztAdmin.getLevel())){
										ztAdmin.setLevel("1");
									}
								}
								ztAdminService.save(ztAdmin);
							}
						}
						
					}else{//没有上级的时候
						ztAdmin.setLastadmin(lastadmin);
						ztAdmin.setYaoqingDate(new Date());
						
						if("1".equals(ztAdmin.getMemtype())){
							if("0".equals(ztAdmin.getLevel())){
								ztAdmin.setLevel("1");
							}
						}
						ztAdminService.save(ztAdmin);
						
					}
				}
			}
		}
		ztGoods.setAdminLevel("0");
		ztGoods.setId(goodsid);
		ztGoods = ztGoodsService.getDetail(ztGoods);
	
		//List<String> imgs = ztGoodsService.getImgs(ztGoods);
		
		String[] imgs=null;
		if(StringUtils.isNotBlank(ztGoods.getContentimgs())){
			imgs=ztGoods.getContentimgs().split("\\|");
		}
		
		List<String> imgsF = new ArrayList<String>();
		String pixP = myProps.getServerPath();
		if(imgs!=null && imgs.length>0){
			for (String s : imgs) {
				if(StringUtils.isNotBlank(s)&&!s.startsWith("http")){
				s = pixP + s;
				}
				imgsF.add(s);
			}
		}
		if (imgsF.size() > 0) {
			ztGoods.setImgs(imgsF);
		}
	
		String content="";
		if(StringUtils.isNotBlank(ztGoods.getContent())){
		 content=ztGoods.getContent().replaceAll("/sk/userfiles/system", myProps.getServerContextPath()+"/sk/userfiles/system");
		}
		ztGoods.setContent(content);
		
		/**************** 颜色  & 规格 START ******************/
		ZtColor ztColor=new ZtColor();
	
		ztGoods.setId(ztGoods.getId());
		ztColor.setZtGoods(ztGoods);
		List<ZtColor> colorList = ztColorService.findList(ztColor);
		
		List<ZtColor> resultColorList = new ArrayList<ZtColor>();
		
		String pathPx=myProps.getServerPath();
		for(ZtColor zc:colorList){
			
			ZtSpecifications ztSpecifications=new ZtSpecifications();
			ztSpecifications.setZtColor(zc);
			List<ZtSpecifications> ztSpecificationsList = ztSpecificationsService.findList(ztSpecifications);
			
			if(ztSpecificationsList!=null && ztSpecificationsList.size()>0){
				List<ZtSpecifications> resultZtSpecificationsList=new ArrayList<ZtSpecifications>();
				
				for(ZtSpecifications zts:ztSpecificationsList){
					ZtGoods ztg=new ZtGoods();
					ztg.setCode(zts.getSku());
					ztg.setAdminLevel("0");
					ztg=ztGoodsService.getByCode(ztg);
					zts.setOrigPrice(ztg.getPrice());
					zc.setZtGoods(ztg);
					if("1".equals(ztg.getSjStatus())){
						if(ztGoods.getCode().equals(zts.getSku())){
							resultZtSpecificationsList.add(zts);
							break;
						}
					}
					
				}
	
			if(zc.getImg()!=null && StringUtils.isNotBlank(zc.getImg())&&!zc.getImg().startsWith("http")){
				zc.setImg(pathPx+zc.getImg());
			}
			if(resultZtSpecificationsList!=null && resultZtSpecificationsList.size()>0){
				
				zc.setZtSpecificationsList(resultZtSpecificationsList);
				resultColorList.add(zc);
				break;
			}
			}else{
				
				ZtGoods ztg=new ZtGoods();
				ztg.setCode(zc.getSku());
				ztg.setAdminLevel("0");
				ztg=ztGoodsService.getByCode(ztg);
				zc.setOrigPrice(ztg.getPrice());
				zc.setZtGoods(ztg);
				if("1".equals(ztg.getSjStatus())){
				if(ztGoods.getCode().equals(zc.getSku())){
					resultColorList.add(zc);
					break;
				}
				
				}
				
				
			}
		}
		
		ztGoods.setColorList(resultColorList);
		/**************** END ******************/
		
		/*ZtComment ztComment=new ZtComment();
		ztGoods.setId(goodsid);
		ztComment.setZtGoods(ztGoods);
		
		List<ZtComment> ztCommentList = ztCommentService.findFirst(ztComment);
		int commentCount=ztCommentService.getCount(ztComment);
		ztGoods.setCommentCount(commentCount);*/
		
		ZtCard ztCard=ztCardService.getOneCard();
		
		ZtTrolley ztTrolley=new ZtTrolley();
		ztTrolley.setZtAdmin(ztAdmin);
		int ztTrolleyCount=ztTrolleyService.getCount(ztTrolley);
		
	//	res.setRecode(ztCommentList);
		
		res.setResult(200L);
		res.setCard(ztCard);
		res.setData(ztGoods);
		res.setMsg(ztTrolleyCount+"");
		res.setRecode(ProductPoolUtils.commentSummarys(ztGoods.getCode()));
		Render.renderString(response, res);
		
	}
	
	/**
	 * 精选推荐更多
	 */

	@RequestMapping(value = "getQuan")
	@ResponseBody
	public void getQuan( HttpServletRequest request,
			HttpServletResponse response, Model model) {
		String paramStr = CommonUtils.getParams(request);

		JSONObject jo = JSONObject.parseObject(paramStr);
		ResultObject res = new ResultObject();
		
		String goodsid = jo.getString("goodsid");
		String adminid=jo.getString("adminid");	
		ZtAdmin ztAdmin=new ZtAdmin();
		ztAdmin.setId(adminid);
		ztAdmin=ztAdminService.get(ztAdmin);
		
		ZtGoods ztGoods = new ZtGoods();
		if(goodsid.length()<16){
			ZtGoodsqcode ztGoodsqcode=new ZtGoodsqcode();
			ztGoodsqcode.setId(goodsid);
			ztGoodsqcode=ztGoodsqcodeService.getByBid(ztGoodsqcode);
			if(ztGoodsqcode!=null){
				
				goodsid=ztGoodsqcode.getGoodsid();
				
			}
		}
		
		ztGoods.setId(goodsid);
		ztGoods = ztGoodsService.get(ztGoods);
		
		/**************************券开始*************************/
		ZtGoodsquan ztGoodsquan=new ZtGoodsquan();
		ztGoodsquan.setZtGoodsid(ztGoods.getId());
		List<ZtGoodsquan> ztQuanList = ztGoodsquanService.findList(ztGoodsquan);
		
		List<ZtQuan> qList =new ArrayList<ZtQuan>();
		
		String goodstemp=ztGoods.getGoodstemp();//0活动     1和2店铺。
		if(!"0".equals(goodstemp)){//店铺
			
		for(ZtGoodsquan zgq:ztQuanList){
			String level=StringUtils.isBlank(ztAdmin.getLevel())?"0":ztAdmin.getLevel();//级别     0就是普通用户
			
			ZtQuan ztQuan=zgq.getZtQuan();//券
			if("4".equals(ztQuan.getType())){//如果是店铺优惠券
				if("3".equals(level)){//普通用户
					if("0".equals(ztQuan.getLevelRange())||"5".equals(ztQuan.getLevelRange())){//发券范围合适
						
						int comptime=com.jeesite.modules.pay.totalPay.util.DateUtils.compare_date(new Date(), ztQuan.getToTime());
						if(comptime==-1&&("0".equals(ztQuan.getUsestatus()))&&ztQuan.getOverCount()>0){//券有效
							
							ZtQuanself ztQuanself=new ZtQuanself();
							
							ztQuanself.setZtQuan(ztQuan);
							ztQuanself.setZtAdmin(ztAdmin);
							ztQuanself.setUsestatus("0");
							ztQuanself=ztQuanselfService.getStatus(ztQuanself);
							if(ztQuanself==null){
								ztQuan.setGetStatus("0");
							}else{
								ztQuan.setGetStatus("1");
							}
							qList.add(ztQuan);
						}
						
						
					}
				}else{//优享会员
					if("0".equals(ztQuan.getLevelRange())||"4".equals(ztQuan.getLevelRange())){
						int comptime=com.jeesite.modules.pay.totalPay.util.DateUtils.compare_date(new Date(), ztQuan.getToTime());
						if(comptime==-1&&("0".equals(ztQuan.getUsestatus()))&&ztQuan.getOverCount()>0){//券有效
							ZtQuanself ztQuanself=new ZtQuanself();
							
							ztQuanself.setZtQuan(ztQuan);
							ztQuanself.setZtAdmin(ztAdmin);
							ztQuanself.setUsestatus("0");
							ztQuanself=ztQuanselfService.getStatus(ztQuanself);
							if(ztQuanself==null){
								ztQuan.setGetStatus("0");
							}else{
								ztQuan.setGetStatus("1");
							}
							qList.add(ztQuan);
						}
					}
				}
			}
			
		}
		}else{//活动商品详情
			boolean isSign=false;
			for(ZtGoodsquan zgq:ztQuanList){
				ZtQuan ztQuan=zgq.getZtQuan();//券
				if("2".equals(ztQuan.getType())){//如果是活动推广券
				int comptime=com.jeesite.modules.pay.totalPay.util.DateUtils.compare_date(new Date(), ztQuan.getToTime());
				if(comptime==-1&&("0".equals(ztQuan.getUsestatus()))&&ztQuan.getOverCount()>0){//券有效
					
					ZtQuanself ztQuanself=new ZtQuanself();				
					ztQuanself.setZtQuan(ztQuan);
					ztQuanself.setZtAdmin(ztAdmin);
					ztQuanself.setUsestatus("0");
					ztQuanself=ztQuanselfService.getStatus(ztQuanself);
					if(ztQuanself!=null){
						ztQuan.setGetStatus("1");
						qList.add(ztQuan);
						isSign=true;
						break;
					}
					
				}	
				
				}else if("1".equals(ztQuan.getType())){//如果是活动免费券
				
					int comptime=com.jeesite.modules.pay.totalPay.util.DateUtils.compare_date(new Date(), ztQuan.getToTime());
					if(comptime==-1&&("0".equals(ztQuan.getUsestatus()))&&ztQuan.getOverCount()>0){//券有效
						
						if("0".equals(ztAdmin.getLevel())){
							
							ZtQuanself ztQuanself=new ZtQuanself();
							
							ztQuanself.setZtQuan(ztQuan);
							ztQuanself.setZtAdmin(ztAdmin);
							
							ztQuanself=ztQuanselfService.getStatus(ztQuanself);
							if(ztQuanself==null){//没有领取
								ztQuan.setGetStatus("0");
								qList.add(ztQuan);
								isSign=true;
								
							}else{//已经领取了
								if("0".equals(ztQuanself.getUsestatus())){//没有使用
									isSign=true;
									ztQuan.setGetStatus("1");
									qList.add(ztQuan);
									
								}else{//已经使用了
									isSign=false;
									
								}
								
							}
							
							
						}
						
					}	

				}
				
			}
			
			
			if(!isSign){
				String level=ztAdmin.getLevel();//级别     3就是普通用户
				for(ZtGoodsquan zgq:ztQuanList){
					ZtQuan ztQuan=zgq.getZtQuan();//券
					if("5".equals(ztQuan.getType())){//如果是活动优惠券
						if("3".equals(level)){//普通用户
							if("0".equals(ztQuan.getLevelRange())||"5".equals(ztQuan.getLevelRange())){//发券范围合适					
								int comptime=com.jeesite.modules.pay.totalPay.util.DateUtils.compare_date(new Date(), ztQuan.getToTime());
								if(comptime==-1&&("0".equals(ztQuan.getUsestatus()))&&ztQuan.getOverCount()>0){//券有效
									ZtQuanself ztQuanself=new ZtQuanself();
									
									ztQuanself.setZtQuan(ztQuan);
									ztQuanself.setZtAdmin(ztAdmin);
									ztQuanself.setUsestatus("0");
									ztQuanself=ztQuanselfService.getStatus(ztQuanself);
									if(ztQuanself==null){
										ztQuan.setGetStatus("0");
									}else{
										ztQuan.setGetStatus("1");
									}
									qList.add(ztQuan);
								}												
							}
						}else{//优享会员
							if("0".equals(ztQuan.getLevelRange())||"4".equals(ztQuan.getLevelRange())){
								int comptime=com.jeesite.modules.pay.totalPay.util.DateUtils.compare_date(new Date(), ztQuan.getToTime());
								if(comptime==-1&&("0".equals(ztQuan.getUsestatus()))&&ztQuan.getOverCount()>0){//券有效
									ZtQuanself ztQuanself=new ZtQuanself();
									
									ztQuanself.setZtQuan(ztQuan);
									ztQuanself.setZtAdmin(ztAdmin);
									ztQuanself.setUsestatus("0");
									ztQuanself=ztQuanselfService.getStatus(ztQuanself);
									if(ztQuanself==null){
										ztQuan.setGetStatus("0");
									}else{
										ztQuan.setGetStatus("1");
									}
									qList.add(ztQuan);
								}
							}
						}
					}
				}
			}
			}
			
		/*****************************券结束*******************************/
		
		res.setData(qList);
		res.setResult(200L);
	
		Render.renderString(response, res);
	}
	
	
	/**
	 * 今日好赚更多
	 */
	@RequestMapping(value = "jinxuanList")
	@ResponseBody
	public void jinxuanList(ZtGoods ztGoods, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		String paramStr = CommonUtils.getParams(request);

		JSONObject jo = JSONObject.parseObject(paramStr);
		ResultObject res = new ResultObject();
		String pageNum = "1";
	
		ZtAdmin ztAdmin=new ZtAdmin();
		if(jo.get("adminid")!=null){
			String adminid=jo.getString("adminid");
			ztAdmin.setId(adminid);
			ztAdmin=ztAdminService.get(ztAdmin);
		}
		
		if(jo.get("pageNum")!=null){
			pageNum=jo.getString("pageNum");
		}

		ztGoods.setAdminLevel("0");
		ztGoods.setPageNum((Integer.valueOf(pageNum) - 1) * 15);
		
		String goodstemp=jo.getString("goodstemp");//1是好赚   2是好物
		//活动更多
		ztGoods.setGoodstemp("5");// 
		
		List<Map> moreGoods = ztGoodsService.findMh(ztGoods);
		moreGoods=jdCheck(moreGoods);

		res.setResult(200L);
		res.setData(moreGoods);
		Render.renderString(response, res);
	}
	
	/**
	 * 今日好赚更多
	 */
	@RequestMapping(value = "moreList")
	@ResponseBody
	public void moreList(ZtGoods ztGoods, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		String paramStr = CommonUtils.getParams(request);

		JSONObject jo = JSONObject.parseObject(paramStr);
		ResultObject res = new ResultObject();
		String pageNum = "2";
	
		ZtAdmin ztAdmin=new ZtAdmin();
		if(jo.get("adminid")!=null){
			String adminid=jo.getString("adminid");
			ztAdmin.setId(adminid);
			ztAdmin=ztAdminService.get(ztAdmin);
		}
		
		if(jo.get("pageNum")!=null){
			pageNum=jo.getString("pageNum");
		}

		ztGoods.setAdminLevel("0");
		ztGoods.setPageNum((Integer.valueOf(pageNum) - 1) * 15);
		
		String goodstemp=jo.getString("goodstemp");//1是好赚   2是好物
		//活动更多
		ztGoods.setGoodstemp(goodstemp);// 
		
		List<Map> moreGoods = ztGoodsService.findMh(ztGoods);
		moreGoods=jdCheck(moreGoods);

		res.setResult(200L);
		res.setData(moreGoods);
		Render.renderString(response, res);
	}
	
	/**
	 * 首页分类更多
	 */
	@RequestMapping(value = "mainFenleiList")
	@ResponseBody
	public void mainFenleiList(ZtGoods ztGoods, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		String paramStr = CommonUtils.getParams(request);

		JSONObject jo = JSONObject.parseObject(paramStr);
		ResultObject res = new ResultObject();
		String pageNum = "2";
	
		ZtAdmin ztAdmin=new ZtAdmin();
		if(jo.get("adminid")!=null){
			String adminid=jo.getString("adminid");
			ztAdmin.setId(adminid);
			ztAdmin=ztAdminService.get(ztAdmin);
		}
		
		if(jo.get("pageNum")!=null){
			pageNum=jo.getString("pageNum");
		}
		if(jo.get("sortType")!=null){
			ztGoods.setSortType(jo.getInteger("sortType"));
		}
		if(StringUtils.isNotBlank(ztAdmin.getLevel())){
			if(ztAdmin.getLevel().equals("0")){
				ztGoods.setAdminLevel("0");
			}else{
				ztGoods.setAdminLevel("1");
			}
		}else {
			ztGoods.setAdminLevel("0");
		}
		ztGoods.setPageNum((Integer.valueOf(pageNum) - 1) * 15);
	
		ztGoods.setGoodstemp("0");// 
		String typeid=jo.getString("typeid");
		ztGoods.setType(typeid+",");
		
		
		List<Map> fenlei = ztGoodsService.findMhByTypeByFirst(ztGoods);
		fenlei = jdCheck(fenlei);
		res.setResult(200L);
		res.setData(fenlei);
		Render.renderString(response, res);
	}
	
	/**
	 * 分类更多
	 */
	@RequestMapping(value = "fenleiList")
	@ResponseBody
	public void fenleiList(ZtGoods ztGoods, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		String paramStr = CommonUtils.getParams(request);

		JSONObject jo = JSONObject.parseObject(paramStr);
		ResultObject res = new ResultObject();
		String pageNum = "2";
	
		ZtAdmin ztAdmin=new ZtAdmin();
		if(jo.get("adminid")!=null){
			String adminid=jo.getString("adminid");
			ztAdmin.setId(adminid);
			ztAdmin=ztAdminService.get(ztAdmin);
		}

		if(jo.get("pageNum")!=null){
			pageNum=jo.getString("pageNum");
		}
		if(jo.get("sortType")!=null){
			ztGoods.setSortType(jo.getInteger("sortType"));
		}

		ztGoods.setPageNum((Integer.valueOf(pageNum) - 1) * 15);
	
		ztGoods.setGoodstemp("0");// 
		String typeid=jo.getString("typeid");
		if(StringUtils.isNotBlank(ztAdmin.getLevel())){
			if(ztAdmin.getLevel().equals("0")){
				ztGoods.setAdminLevel("0");
			}else{
				ztGoods.setAdminLevel("1");
			}
		}else {
			ztGoods.setAdminLevel("0");
		}
		GoodsType gt=new GoodsType();
		gt.setGoodsCode(","+typeid+",");
		//商品二级分类列表
		List<Map> typeTwoList = goodsTypeService.getTwoList(gt);
		
		ztGoods.setType(typeTwoList.get(0).get("id").toString());//这里如果list为空会报错
		
		
		List<Map> fenlei = ztGoodsService.findMhByType(ztGoods);
		fenlei = jdCheck(fenlei);
		Map m=new HashMap();
		m.put("fenlei", fenlei);
		m.put("typeList",typeTwoList);
		res.setResult(200L);
		res.setData(m);
		Render.renderString(response, res);
	}
	
	
	/**
	 * 分类更多
	 */
	@RequestMapping(value = "fenleiMoreList")
	@ResponseBody
	public void fenleiMoreList(ZtGoods ztGoods, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		String paramStr = CommonUtils.getParams(request);

		JSONObject jo = JSONObject.parseObject(paramStr);
		ResultObject res = new ResultObject();
		String pageNum = "2";
	
		ZtAdmin ztAdmin=new ZtAdmin();
		if(jo.get("adminid")!=null){
			String adminid=jo.getString("adminid");
			ztAdmin.setId(adminid);
			ztAdmin=ztAdminService.get(ztAdmin);
		}
		
		if(jo.get("pageNum")!=null){
			pageNum=jo.getString("pageNum");
		}
		if(jo.get("sortType")!=null){
			ztGoods.setSortType(jo.getInteger("sortType"));
		}
		ztGoods.setPageNum((Integer.valueOf(pageNum) - 1) * 15);
	
		ztGoods.setGoodstemp("0");// 
		String typeid=jo.getString("typeid");
		ztGoods.setType(","+typeid+",");
		if(StringUtils.isNotBlank(ztAdmin.getLevel())){
			if(ztAdmin.getLevel().equals("0")){
				ztGoods.setAdminLevel("0");
			}else{
				ztGoods.setAdminLevel("1");
			}
		}else {
			ztGoods.setAdminLevel("0");
		}
		
		List<Map> fenlei = ztGoodsService.findMhByType(ztGoods);
		fenlei = jdCheck(fenlei);
		res.setResult(200L);
		res.setData(fenlei);
		Render.renderString(response, res);
		
	}

	
	
	/**
	 * 品牌专场
	 */
	@RequestMapping(value = "brand")
	@ResponseBody
	public void brand(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		String paramStr = CommonUtils.getParams(request);

		JSONObject jo = JSONObject.parseObject(paramStr);
		ResultObject res = new ResultObject();
		String pageNum = "1";
	
		ZtAdmin ztAdmin=new ZtAdmin();
		if(jo.get("adminid")!=null){
			String adminid=jo.getString("adminid");
			ztAdmin.setId(adminid);
			ztAdmin=ztAdminService.get(ztAdmin);
		}
		
		if(jo.get("pageNum")!=null){
			pageNum=jo.getString("pageNum");
		}
		String brandid=jo.getString("brandid");//品牌专场id
		Brand brand=new Brand();
		brand.setId(brandid);
		brand = brandService.get(brand);
		if(StringUtils.isNotBlank(brand.getImg())&&!brand.getImg().startsWith("http")){
			brand.setImg(myProps.getServerPath()+brand.getImg());
		}
		if(StringUtils.isNotBlank(brand.getContentimg())&&!brand.getContentimg().startsWith("http")){
			brand.setContentimg(myProps.getServerPath()+brand.getContentimg());
		}
		
		ZtGoods ztGoods =new ZtGoods();
		ztGoods.setAdminLevel("0");
		ztGoods.setPageNum((Integer.valueOf(pageNum) - 1) * 15);
	
		ztGoods.setGoodstemp("3");// 
		ztGoods.setBrandid(brandid);
		List<Map> fenlei = ztGoodsService.findMh(ztGoods);
		fenlei = jdCheck(fenlei);
		Map m=new HashMap();
		m.put("goodslist", fenlei);
		m.put("brand",brand);
		res.setResult(200L);
		res.setData(m);
		Render.renderString(response, res);
		
	}
	

	/**
	 * go
	 */

	@RequestMapping(value = "listOrder")
	@ResponseBody
	public void listOrder(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		String paramStr = CommonUtils.getParams(request);
		Map map = new HashMap();

		JSONObject jo = JSONObject.parseObject(paramStr);
		ResultObject res = new ResultObject();

		ZtOrder ztOrder = new ZtOrder();
		ztOrder.setOrderstatus("1");
		List<ZtOrder> ztOrderList = ztOrderService.findListByNew(ztOrder);
		List<Map> mapList = new ArrayList<Map>();
		if (ztOrderList != null && ztOrderList.size() > 0) {
			Map m = new HashMap();
			for (ZtOrder zo : ztOrderList) {
				String goods = zo.getGoods();
				if (StringUtils.isNotBlank(goods)) {

					String[] goodsArr = goods.split("&");

					String[] goodsD = goodsArr[0].split("@");

					ZtGoods ztGoods = ztGoodsService.get(goodsD[0]);

					String title = ztGoods.getTitle();
					m.put("goodsTitle", title);

				}
				m.put("name", zo.getZtAdmin().getName());
				mapList.add(m);

			}
		}
		res.setResult(200L);
		res.setData(mapList);
		Render.renderString(response, res);
	}
	
	/**
	 * 获取商品详情的海报图
	 */
	@RequestMapping(value = "getGoodsQcode")
	@ResponseBody
	public void getGoodsQcode(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		String paramStr = CommonUtils.getParams(request);
		
		JSONObject jo = JSONObject.parseObject(paramStr);
		String adminid=jo.getString("adminid");
		String goodsid=jo.getString("goodsid");
		
		if(StringUtils.isNotBlank(goodsid) && goodsid.length()<10){
			ZtGoodsqcode ztGoodsqcode=new ZtGoodsqcode();
			ztGoodsqcode.setBid(Integer.valueOf(goodsid));
			ztGoodsqcode=ztGoodsqcodeService.getByBid(ztGoodsqcode);
			if(ztGoodsqcode!=null){
				
				goodsid=ztGoodsqcode.getGoodsid();
				
			}
		}
		
		ZtGoods ztGoods=new ZtGoods();
		ztGoods.setId(goodsid);
		ztGoods=ztGoodsService.get(ztGoods);
		
		ZtGoodsqcode ztGoodsqcode=new ZtGoodsqcode();
		ztGoodsqcode.setGoodsid(goodsid);
		ztGoodsqcode.setAdminid(adminid);
		ztGoodsqcode.setType(0);
		ztGoodsqcode=ztGoodsqcodeService.getByGoodsAdminid(ztGoodsqcode);
		
		String resultImg="";
		
		if(ztGoodsqcode!=null && StringUtils.isNotBlank(ztGoodsqcode.getImg())){
			resultImg=ztGoodsqcode.getImg();
		}else{
			
			ztGoodsqcode=new ZtGoodsqcode();
			ztGoodsqcode.setGoodsid(goodsid);
			ztGoodsqcode.setAdminid(adminid);
			ztGoodsqcode.setCreateDate(new Date());
			ztGoodsqcode.setType(0);
			
			ztGoodsqcodeService.saveinsert(ztGoodsqcode);
			
			resultImg=s(request,ztGoodsqcode,ztGoods,adminid);
			
			
		}
		
		ResultObject res = new ResultObject();
		
		res.setResult(200L);
		res.setData(resultImg);
		Render.renderString(response, res);
		
	}
	
	
	/**
	 * 首页的海报图
	 */

	@RequestMapping(value = "getMainPoster")
	@ResponseBody
	public void getMainPoster(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		String paramStr = CommonUtils.getParams(request);
		
		JSONObject jo = JSONObject.parseObject(paramStr);
		String adminid=jo.getString("adminid");
		int type=0;
		if(jo.get("type")!=null){//0主页    1掌柜推荐
			type=Integer.valueOf(jo.get("type").toString());
		}
		
		ZtGoodsqcode ztGoodsqcode=new ZtGoodsqcode();
		
		ztGoodsqcode.setAdminid(adminid);
		if(type==0){
		ztGoodsqcode.setType(1);
		}else{
			ztGoodsqcode.setType(2);
		}
		ztGoodsqcode.setGoodsid("1");
		ztGoodsqcode=ztGoodsqcodeService.getByGoodsAdminid(ztGoodsqcode);
		
		String resultImg="";
		
		if(ztGoodsqcode!=null && StringUtils.isNotBlank(ztGoodsqcode.getImg())){
			resultImg=ztGoodsqcode.getImg();
		}else{
			
			ztGoodsqcode=new ZtGoodsqcode();
			
			ztGoodsqcode.setAdminid(adminid);
			ztGoodsqcode.setCreateDate(new Date());
			if(type==0){
				ztGoodsqcode.setType(1);
			}else{
				ztGoodsqcode.setType(2);
			}
			ztGoodsqcode.setGoodsid("1");
			ztGoodsqcodeService.saveinsert(ztGoodsqcode);
			
			resultImg=mainPoster(request,ztGoodsqcode,adminid,type);
		}
		
		ResultObject res = new ResultObject();
		
		res.setResult(200L);
		res.setData(resultImg);
		Render.renderString(response, res);
		
	}
	
	
	
	
	private String resultImg;
	private String imgName;
	
	/**
	 * 商品详情的海报生成
	 * @param request
	 * @param ztGoodsqcode
	 * @param ztGoods
	 * @param adminid
	 * @return
	 */
	public String s(HttpServletRequest request,ZtGoodsqcode ztGoodsqcode,ZtGoods ztGoods,String adminid){
		String sceneStr="rz&"+ztGoodsqcode.getBid();

		String xcxQcode="";
		try {
			
				 xcxQcode = TTest.getQcodeImg(request,sceneStr,"pages/goodDel/goodDel");//pages/goodDel/goodDel
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		Picture1 tt = new Picture1();  
	
        BufferedImage b = tt.loadImageLocal(xcxQcode);//小程序
        
		String path = request.getSession().getServletContext().getRealPath("")
					+ CommonPath.getDestFolder(request) + "/";
       // String path="D:\\bc\\";
        
	         imgName = "pro" + adminid + System.currentTimeMillis()
					+ ".png";
			String imgPath=path+imgName;
			
		
		File file = new File(path);
		if (!file.exists()) {
			try {
				file.mkdirs();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		String schemeUrl = myProps.getServerPath()+"/sk";
		resultImg=(schemeUrl+imgPath).replace("/sk/usr/local/tomcat8081/webapps", "");// 图片地址
		
		
		ZtAdmin ztAdmin=new ZtAdmin();
		ztAdmin.setId(adminid);
		ztAdmin=ztAdminService.get(ztAdmin);
		
		String uname="好友掌柜官方小店";
		String headimgurl=Contacts.logoimg;
		if(!"0".equals(ztAdmin.getLevel())){//掌柜
			uname=ztAdmin.getUsername()+"的小店";
			headimgurl=ztAdmin.getHeadimgurl();
		}else{
			if(StringUtils.isNotBlank(ztAdmin.getLastadmin())){
				ZtAdmin lastadmin=getLastLevelAdmin(ztAdmin);
				if(lastadmin!=null && StringUtils.isNotBlank(lastadmin.getId())){
					uname=lastadmin.getUsername()+"的小店";
					headimgurl=lastadmin.getHeadimgurl();
				}
			}
		}

        tt.writeImageLocal(imgPath, tt.modifyImagetogeter(b, ztGoods,uname,headimgurl));
        
        ExecutorService cachedThreadPool=ProductPoolUtils.cachedThreadPool;
        if(cachedThreadPool==null){
        	cachedThreadPool=Executors.newCachedThreadPool();
        }

				cachedThreadPool.execute(new Runnable() {			
					@Override
					public void run() {
						// TODO Auto-generated method stub
						try{
							AliRequestUtils.uploadFile(resultImg, imgName,"linghu365");
						}catch(Exception ex){}
					}
				});
			
        
        ztGoodsqcode.setImg("https://linghu365.oss-cn-shenzhen.aliyuncs.com/"+imgName);
        ztGoodsqcodeService.updateSave(ztGoodsqcode);
        
        return resultImg;
	}
	/**
	 * 首页海报生成
	 * @param request
	 * @param ztGoodsqcode
	 * @param ztGoods
	 * @param adminid
	 * @return
	 */
	public String mainPoster(HttpServletRequest request,ZtGoodsqcode ztGoodsqcode,String adminid,int type){
		
		String sceneStr="lastadmin="+adminid;
		
		String xcxQcode="";
		try {
			xcxQcode = TTest.getQcodeImg(request,sceneStr,"pages/index/index");
			
		} catch (Exception e1) {
			e1.printStackTrace();
		}

		Picture2 tt = new Picture2();  
	
        BufferedImage b = tt.loadImageLocal(xcxQcode);//小程序
        
		String path = request.getSession().getServletContext().getRealPath("")
					+ CommonPath.getDestFolder(request) + "/";
        
	         imgName = "index" + adminid + System.currentTimeMillis()
					+ ".png";
			String imgPath=path+imgName;
			
		File file = new File(path);
		
		if (!file.exists()) {
			try {
				file.mkdirs();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		String schemeUrl = myProps.getServerPath()+"/sk";
		 resultImg=(schemeUrl+imgPath).replace("/sk/usr/local/tomcat8081/webapps", "");// 图片地址
		
		
		ZtAdmin ztAdmin=new ZtAdmin();
		ztAdmin.setId(adminid);
		ztAdmin=ztAdminService.get(ztAdmin);
		
		String uname=ztAdmin.getUsername();
		String headimgurl=ztAdmin.getHeadimgurl();
		if(!"0".equals(ztAdmin.getLevel())){//掌柜
			uname=ztAdmin.getUsername()+"掌柜的小店";
			headimgurl=ztAdmin.getHeadimgurl();
		}else{
			if(StringUtils.isNotBlank(ztAdmin.getLastadmin())){
				
				ZtAdmin lastadmin=getLastLevelAdmin(ztAdmin);
				if(lastadmin!=null && StringUtils.isNotBlank(lastadmin.getId())){
					uname=lastadmin.getUsername()+"掌柜的小店";
					headimgurl=lastadmin.getHeadimgurl();
				}
				
			}
		}
	
		if(type==0){//主页
        tt.writeImageLocal(imgPath, tt.modifyImagetogeter(b,uname,headimgurl));
		}else{//推荐
        tt.writeImageLocal(imgPath, tt.zgskImagetogeter(b,uname,headimgurl,xcxQcode));
		}
      
		 ExecutorService cachedThreadPool=ProductPoolUtils.cachedThreadPool;
	        if(cachedThreadPool==null){
	        	cachedThreadPool=Executors.newCachedThreadPool();
	        }

				cachedThreadPool.execute(new Runnable() {			
					@Override
					public void run() {
						// TODO Auto-generated method stub
						try{
							AliRequestUtils.uploadFile(resultImg, imgName,"linghu365");
						}catch(Exception ex){}
					}
				});
			
        ztGoodsqcode.setImg("https://linghu365.oss-cn-shenzhen.aliyuncs.com/"+imgName);
        ztGoodsqcodeService.updateSave(ztGoodsqcode);
        
        return resultImg;
	}
	
	 public static String getminiqrQr(HttpServletRequest request,String sceneStr) {
		 //String reqeustUrl="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxb0283c0ffa73cd90&secret=4493a55897cda0b79dbac61651d24e5d";
	
		 String APPID="wx819630a1fe8a51fb";
		     String APP_SECRECT="9addae533648463ebeacc1013fb64980";	
		 String reqeustUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+APPID+"&secret="+APP_SECRECT;
		 String accessToken="";
			
			try{
			StringBuffer s = CommonUtil.httpsRequest(reqeustUrl,"GET", null);
			if(s!=null){
				//JSONObject jsonObject
				//s.toString();
				JSONObject jsonObject=JSONObject.parseObject(s.toString());
				if (jsonObject != null) {
					 accessToken=jsonObject.getString("access_token");
					
					
				}
			}
			}catch(Exception ex){
				
			}
			 String path = request.getSession().getServletContext().getRealPath("")
	    				+ CommonPath.getDestFolder(request) + "/";
	            String fileName = UUID.randomUUID().toString() + ".png";
	    		String deshPath=path+fileName;
		 
	        RestTemplate rest = new RestTemplate();
	        InputStream inputStream = null;
	        OutputStream outputStream = null;
	        try {//ACCESS_TOKEN
	            String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+accessToken;
	            Map<String,Object> param = new HashMap<>();
	            param.put("scene", sceneStr);
	            param.put("page", "pages/promoteGoodDetail/promoteGoodDetail");
	            //'pages/promoteGoodDetail/promoteGoodDetail?goodId=' + this.data.goodId + '&adminid' + adminid,
	            param.put("width", 430);
	            param.put("auto_color", false);
	            Map<String,Object> line_color = new HashMap<>();
	            line_color.put("r", 0);
	            line_color.put("g", 0);
	            line_color.put("b", 0);
	            param.put("line_color", line_color);
	            System.out.println("调用生成微信URL接口传参:" + param);
	            
	            MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
	            HttpEntity requestEntity = new HttpEntity(param, headers);
	            ResponseEntity<byte[]> entity = rest.exchange(url, HttpMethod.POST, requestEntity, byte[].class, new Object[0]);
	            System.out.println("调用小程序生成微信永久小程序码URL接口返回结果:" + entity.getBody());
	            byte[] result = entity.getBody();
	            System.out.println(Base64.encodeBase64String(result));
	            inputStream = new ByteArrayInputStream(result);

	           // deshPath="D:\\bc\\kkk.png";
	            
	           
	    		File file = new File(deshPath);
	    		
	            
	            if (!file.exists()){
	                file.createNewFile();
	            }
	            outputStream = new FileOutputStream(file);
	            int len = 0;
	            byte[] buf = new byte[1024];
	            while ((len = inputStream.read(buf, 0, 1024)) != -1) {
	                outputStream.write(buf, 0, len);
	            }
	            outputStream.flush();
	        } catch (Exception e) {
	            System.out.println("调用小程序生成微信永久小程序码URL接口异常"+e);
	        } finally {
	            if(inputStream != null){
	                try {
	                    inputStream.close();
	                } catch (IOException e) {
	                    e.printStackTrace();
	                }
	            }
	            if(outputStream != null){
	                try {
	                    outputStream.close();
	                } catch (IOException e) {
	                    e.printStackTrace();
	                }
	            }
	        }
	        return deshPath;    
	    }
	
	 private void getQuan(String goodsid,ZtAdmin ztAdmin){
			ZtGoodsquan ztGoodsquan=new ZtGoodsquan();
			ztGoodsquan.setZtGoodsid(goodsid);
			List<ZtGoodsquan> ztQuanList = ztGoodsquanService.findList(ztGoodsquan);
			
			ZtGoods ztGoods=ztGoodsService.get(goodsid);
			
			String goodstemp=ztGoods.getGoodstemp();//0活动     1和2店铺。
			
			if("0".equals(goodstemp)){//活动
				ZtAdmin lastadmin=ztAdminService.get(ztAdmin.getLastadmin());
				String lastLevel=String.valueOf(Integer.valueOf(lastadmin.getLevel())+1);//0特级   1一级    2二级
			for(ZtGoodsquan zgq:ztQuanList){
						
				ZtQuan ztQuan=zgq.getZtQuan();//券
				if("2".equals(ztQuan.getType())){//如果是活动优惠券  
					if(lastLevel.equals(ztQuan.getLevelRange())){//1特级代理   2一级代理    3二级代理
						
						ZtQuanself ztQuanself=new ZtQuanself();
						ztQuanself.setZtQuan(ztQuan);
						ztQuanself.setZtAdmin(ztAdmin);
						ztQuanself.setUsestatus("0");
						ztQuanself=ztQuanselfService.getByAdmin(ztQuanself);
						if(ztQuanself!=null){
							break;
						}else{
						
						ztQuanself=new ZtQuanself();
						ztQuanself.setZtQuan(ztQuan);
						ztQuanself.setZtAdmin(ztAdmin);
						ztQuanself.setCreateDate(new Date());
						ztQuanselfService.save(ztQuanself);
						break;
						}
					}
				}
				
			}
			}
		}
	 
	 public ZtAdmin getLastLevelAdmin(ZtAdmin ztAdmin){
		 ZtAdmin lastadmin=new ZtAdmin();
		 if(ztAdmin !=null && StringUtils.isNotBlank(ztAdmin.getLastadmin())){
			lastadmin.setId(ztAdmin.getLastadmin());
			lastadmin=ztAdminService.getZg(lastadmin);
			if(lastadmin!=null && StringUtils.isNotBlank(lastadmin.getId())){
				
				if(!"0".equals(lastadmin.getLevel())){
					return lastadmin; 
				}else{
					getLastTwoLevelAdmin(lastadmin);
				}
				
			}else{
				
				return lastadmin;
			}
		 }
			return lastadmin;
	 }
	 
	 public ZtAdmin getLastTwoLevelAdmin(ZtAdmin ztAdmin){
		 ZtAdmin lastadmin=new ZtAdmin();
		 if(ztAdmin !=null && StringUtils.isNotBlank(ztAdmin.getLastadmin())){
			lastadmin.setId(ztAdmin.getLastadmin());
			lastadmin=ztAdminService.getZg(lastadmin);
			if(lastadmin!=null && StringUtils.isNotBlank(lastadmin.getId())){
				
				if(!"0".equals(lastadmin.getLevel())){
					return lastadmin; 
				}else{
					return lastadmin;
				}
				
			}else{
				
				return lastadmin;
			}
		 }
			return lastadmin;
	 }

	@RequestMapping("getGoodsType")
	public Map<String,Object> getGoodsType(@RequestParam("type") String type){
		Map<String,Object> map =new HashMap<>();
		map.put("result",501);
		map.put("data","异常");
		if(StringUtils.isNotBlank(type)){
			List<Map> goodsTypeList = goodsTypeService.getGoodsTypeList(type);
			map.put("result",200);
			map.put("data",goodsTypeList);
		}else {
			List<Map> typeList = goodsTypeService.getList();
			if(typeList.size()>0){
				List<Map> goodsTypeList = goodsTypeService.getGoodsTypeList(typeList.get(0).get("id").toString());
				typeList.get(0).put("list",goodsTypeList);
			}
			map.put("result",200);
			map.put("data",typeList);
		}
		return map;
	}
	/**
	 *  京东产品校验
	 * @param moreGoods
	 * @return
	 */
	private List<Map> jdCheck(List<Map> moreGoods) {
		String skus = "";
		for (Map m : moreGoods) {
			if (StringUtils.isNotBlank(m.get("img").toString()) && !m.get("img").toString().startsWith("http")) {
				m.put("img", myProps.getServerPath() + m.get("img").toString());
			}
			if (Integer.valueOf(m.get("spotid").toString()) == 1) {//京东
				skus += m.get("code").toString() + ",";
			}
		}
		if (StringUtils.isNotBlank(skus)) {
			if (skus.length() > 1) {
				skus = skus.substring(0, skus.length() - 1);
				moreGoods = ProductPoolUtils.productSkuCheck(skus, moreGoods);//可售
			}
			skus = "";
			for (Map m : moreGoods) {
				if (Integer.valueOf(m.get("spotid").toString()) == 1) {//京东
					skus += m.get("code").toString() + ",";
				}
			}
			if (skus.length() > 1) {
				moreGoods = ProductPoolUtils.productState(skus.substring(0, skus.length() - 1), moreGoods);//上下架
			}
		}
		return moreGoods;
	}
}